package samplebasic

import sweet.mvc.api._
import sweet.helper.OrderedMap
import sweet.helper.JDate

class FormDemoController extends FormController {
  
  val form = new Form("demo"){
    addField("mytext1", "A required text input", StringConverter){
      VRequired()++VLen(1, 64, "You can't enter more than 64 characters.")  
    }    
    addField("mytext2", "An optional number input", IntConverter){
      VOptional()++VLen(1, 2, "You can't enter more than 2 digits.")++
      VInt(-50, 50, "Please enter number -50 to 50.")
    }    
    addField("mytext3", "An optional decimal input", DoubleConverter){
      VOptional()++VLen(1, 12, "You can't enter more than 12 digits(including the decimal).")++
      VDouble(0.00, 10000.00, "Please enter number 0.00 to 1000.00.")
    }    
    addField("mytext4", "An optional date input (YYYY-MM-DD)", DateConverter){
      VOptional()++VLen(1, 10, "You can't enter more than 10 characters.")++
      VDateAfter(JDate.DATE_FMT, JDate.now, "You must enter a future date.")
    }   
    addField("mytext5", "An optional datetime input (YYYY-MM-DD HH:MM:SS)", DatetimeConverter){
      VOptional()++VLen(1, 19, "You can't enter more than 19 characters.")++
      VDateBefore(JDate.DATETIME_FMT, JDate.now, "You must enter a past date.")
    }     
    addField("mypassword", "An optional password input", StringConverter){
      VOptional()++VLen(1, 16, "You can't enter more than 64 characters.")  
    }    
    addMultiValuesField("myradio1", "A required selection", StringConverter, VRequired()){
      OrderedMap("Male"->"male", "Female"->"female")
    }
    addMultiValuesField("myradio2", "An optional selection", StringConverter, VOptional()){
      OrderedMap("Scala"->"scala", "Java"->"java", "Other"->"other")
    }
    addMultiValuesField("mycheck1", "A required multi selections", StringConverter, VRequired()){
      OrderedMap("Scala"->"scala", "Java"->"java", "Other"->"other")
    }
    addMultiValuesField("mycheck2", "An optional multi selections", StringConverter, VOptional()){
      OrderedMap("Mac OSX"->"macosx", "Linux"->"linux", "Windows"->"windows", "Other"->"other")
    }
    addMultiValuesField("mycheck3", "A required multi selections with defaults", StringConverter, VRequired()){
      OrderedMap("SunMicrosystems"->"sun", "Google"->"google", "IBM"->"ibm", "Apple"->"apple",  "Microsoft"->"ms")
    }
    addMultiValuesField("myselect1", "A required drop down selection", StringConverter, VRequired()){
      OrderedMap("Pick One..."->"", "SunMicrosystems"->"sun", "Google"->"google", "IBM"->"ibm", "Apple"->"apple",  "Microsoft"->"ms")
    }
    addMultiValuesField("myselect2", "A required multi-selection", StringConverter, VOptional()){
      OrderedMap("Honda"->"honda", "Toyota"->"toyota", "Ford"->"ford", "Nissan"->"nissan")
    }
    addField("mytextarea", "A optional textarea input", StringConverter){
      VOptional()++VLen(1, 1024, "You can't enter more than 1024 characters.")  
    } 
    
    override def initialFieldValue(multiValKey: String, field: Field): String = {
      if(field.name == "mycheck3" && multiValKey == "Google"){
        "google"
      }else if(field.name == "mycheck3" && multiValKey == "Apple"){
        "apple"
      }else{
        super.initialFieldValue(multiValKey, field)
      }
    }
  }
    
  handleForm("/index", form){ 
    new FormHandler(){
      def viewForm(formData: FormData, req: Req) =  HandlerNameView()
      def processForm(formData: FormData, req: Req) = HandlerNameView("submitSuccessful"->true)
    }
  }
}
